package ${packagePrefix}.common.exception;

<#if generateComments>
/**
 * 业务异常类
 * <p>
 * 表示业务逻辑异常，通常是由于用户操作或业务规则导致的异常。
 * </p>
 * 
 * @author Auto Generated
 */
</#if>
public class BusinessException extends BaseException {

    public BusinessException(ErrorCode errorCode, String message) {
        super(errorCode, message);
    }

    public BusinessException(ErrorCode errorCode, String message, Throwable cause) {
        super(errorCode, message, cause);
    }
}

<#if generateComments>
/**
 * 数据不存在异常
 * <p>
 * 当请求的数据在系统中不存在时抛出此异常。
 * </p>
 */
</#if>
class DataNotFoundException extends BusinessException {

    public DataNotFoundException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_NOT_FOUND, message);
    }

    public DataNotFoundException(String entityName, Long id) {
        super(ErrorCode.${domainObjectName?upper_case}_NOT_FOUND, entityName + " ID:" + id + " 不存在");
    }
}

<#if generateComments>
/**
 * 数据重复异常
 * <p>
 * 当尝试创建重复数据时抛出此异常。
 * </p>
 */
</#if>
class DuplicateDataException extends BusinessException {

    public DuplicateDataException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_ALREADY_EXISTS, message);
    }

    public DuplicateDataException(String entityName, String field, String value) {
        super(ErrorCode.${domainObjectName?upper_case}_ALREADY_EXISTS, entityName + " 的 " + field + ":" + value + " 已存在");
    }
}

<#if generateComments>
/**
 * 数据无效异常
 * <p>
 * 当数据不符合业务规则或格式要求时抛出此异常。
 * </p>
 */
</#if>
class InvalidDataException extends BusinessException {

    public InvalidDataException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_DATA_INVALID, message);
    }

    public InvalidDataException(String field, String reason) {
        super(ErrorCode.${domainObjectName?upper_case}_DATA_INVALID, "字段 " + field + " 无效: " + reason);
    }
}

<#if generateComments>
/**
 * 状态异常
 * <p>
 * 当实体状态不符合操作要求时抛出此异常。
 * </p>
 */
</#if>
class StatusErrorException extends BusinessException {

    public StatusErrorException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_STATUS_ERROR, message);
    }

    public StatusErrorException(String entityName, Integer currentStatus, Integer requiredStatus) {
        super(ErrorCode.${domainObjectName?upper_case}_STATUS_ERROR, 
              entityName + " 当前状态 " + currentStatus + "，需要状态 " + requiredStatus);
    }
}

<#if generateComments>
/**
 * 权限拒绝异常
 * <p>
 * 当用户没有足够权限执行操作时抛出此异常。
 * </p>
 */
</#if>
class PermissionDeniedException extends BusinessException {

    public PermissionDeniedException(String message) {
        super(ErrorCode.PERMISSION_DENIED, message);
    }

    public PermissionDeniedException(String operation, String resource) {
        super(ErrorCode.PERMISSION_DENIED, "无权执行操作 " + operation + " 于资源 " + resource);
    }
}

<#if generateComments>
/**
 * 认证异常
 * <p>
 * 当用户认证失败时抛出此异常。
 * </p>
 */
</#if>
class AuthenticationException extends BusinessException {

    public AuthenticationException(String message) {
        super(ErrorCode.UNAUTHORIZED, message);
    }

    public AuthenticationException() {
        super(ErrorCode.UNAUTHORIZED, "认证失败或未认证");
    }
}

<#if generateComments>
/**
 * 外部系统异常
 * <p>
 * 当调用外部系统失败时抛出此异常。
 * </p>
 */
</#if>
class ExternalSystemException extends BusinessException {

    public ExternalSystemException(String message) {
        super(ErrorCode.THIRD_PARTY_ERROR, message);
    }

    public ExternalSystemException(String system, String details) {
        super(ErrorCode.THIRD_PARTY_ERROR, "调用外部系统 " + system + " 失败: " + details);
    }
    
    public ExternalSystemException(String message, Throwable cause) {
        super(ErrorCode.THIRD_PARTY_ERROR, message, cause);
    }
}

<#if generateComments>
/**
 * 并发操作异常
 * <p>
 * 当并发操作导致冲突时抛出此异常。
 * </p>
 */
</#if>
class ConcurrentOperationException extends BusinessException {

    public ConcurrentOperationException(String message) {
        super(ErrorCode.DUPLICATE_SUBMISSION, message);
    }
    
    public ConcurrentOperationException(String entityName, Long id) {
        super(ErrorCode.DUPLICATE_SUBMISSION, entityName + " ID:" + id + " 正在被其他操作修改");
    }
}

<#if generateComments>
/**
 * 操作不允许异常
 * <p>
 * 当当前状态下不允许执行某操作时抛出此异常。
 * </p>
 */
</#if>
class OperationNotAllowedException extends BusinessException {

    public OperationNotAllowedException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_OPERATION_NOT_ALLOWED, message);
    }
    
    public OperationNotAllowedException(String operation, String reason) {
        super(ErrorCode.${domainObjectName?upper_case}_OPERATION_NOT_ALLOWED, "操作 " + operation + " 不允许: " + reason);
    }
}

<#if generateComments>
/**
 * 文件操作异常
 * <p>
 * 当文件上传、下载或处理失败时抛出此异常。
 * </p>
 */
</#if>
class FileOperationException extends BusinessException {

    public FileOperationException(String message) {
        super(ErrorCode.PARAM_ERROR, message);
    }
    
    public FileOperationException(String operation, String fileName, String reason) {
        super(ErrorCode.PARAM_ERROR, "文件 " + fileName + " " + operation + " 失败: " + reason);
    }
    
    public FileOperationException(String message, Throwable cause) {
        super(ErrorCode.PARAM_ERROR, message, cause);
    }
}

<#if specialRequirements?? && specialRequirements?contains("可删除")>
<#if generateComments>
/**
 * 数据不可删除异常
 * <p>
 * 当尝试删除不允许删除的数据时抛出此异常。
 * </p>
 */
</#if>
class CannotDeleteException extends BusinessException {

    public CannotDeleteException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_CANNOT_DELETE, message);
    }
    
    public CannotDeleteException(String entityName, Long id, String reason) {
        super(ErrorCode.${domainObjectName?upper_case}_CANNOT_DELETE, entityName + " ID:" + id + " 不可删除: " + reason);
    }
}
</#if>

<#if specialRequirements?? && specialRequirements?contains("使用中")>
<#if generateComments>
/**
 * 数据使用中异常
 * <p>
 * 当尝试修改或删除正在使用中的数据时抛出此异常。
 * </p>
 */
</#if>
class DataInUseException extends BusinessException {

    public DataInUseException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_IN_USE, message);
    }
    
    public DataInUseException(String entityName, Long id, String usedBy) {
        super(ErrorCode.${domainObjectName?upper_case}_IN_USE, entityName + " ID:" + id + " 正在被 " + usedBy + " 使用");
    }
}
</#if>

<#if specialRequirements?? && specialRequirements?contains("审批")>
<#if generateComments>
/**
 * 审批状态异常
 * <p>
 * 当实体的审批状态不符合操作要求时抛出此异常。
 * </p>
 */
</#if>
class ApprovalStatusException extends BusinessException {

    public ApprovalStatusException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_APPROVAL_STATUS_ERROR, message);
    }
    
    public ApprovalStatusException(String entityName, Integer currentStatus, String operation) {
        super(ErrorCode.${domainObjectName?upper_case}_APPROVAL_STATUS_ERROR, 
              entityName + " 当前审批状态 " + currentStatus + "，不能执行 " + operation + " 操作");
    }
}

<#if generateComments>
/**
 * 需要审批异常
 * <p>
 * 当操作需要审批流程时抛出此异常。
 * </p>
 */
</#if>
class ApprovalRequiredException extends BusinessException {

    public ApprovalRequiredException(String message) {
        super(ErrorCode.${domainObjectName?upper_case}_APPROVAL_REQUIRED, message);
    }
    
    public ApprovalRequiredException(String entityName, String operation) {
        super(ErrorCode.${domainObjectName?upper_case}_APPROVAL_REQUIRED, 
              entityName + " 的 " + operation + " 操作需要审批");
    }
}
</#if> 